<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单编号" prop="orders">
        <el-input
          v-model="queryParams.orders"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="!isBelongMatch" label="活动名称" prop="activeName">
        <el-input
          v-model="queryParams.activeName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票类型" prop="invoiceType">
        <el-select v-model="queryParams.invoiceType" placeholder="选择发票类型" clearable @change="handleQuery">
          <el-option label="企业" value="0" />
          <el-option label="个人/非企业单位" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceTitle">
        <el-input
          v-model="queryParams.invoiceTitle"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税号" prop="invoiceTfn">
        <el-input
          v-model="queryParams.invoiceTfn"
          placeholder="请输入发票税号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票形式" prop="invoiceForm">
        <el-select v-model="queryParams.invoiceForm" placeholder="选择发票形式" clearable @change="handleQuery">
          <el-option label="电子发票" value="0" />
          <el-option label="纸质普通" value="1" />
          <el-option label="纸质专票" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="发票状态" prop="isInvoice">
        <el-select v-model="queryParams.isInvoice" clearable placeholder="选择发票状态" @change="handleQuery">
          <el-option label="未开票" value="0" />
          <el-option label="已开票" value="1" />
          <el-option label="取消开票" value="2" />
          <el-option label="已寄出" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="handTime">
        <el-date-picker
          v-model="queryParams.handTime"
          clearable
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="warning" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          :disabled="multiple"
          @click="handleChangeStatusAll"
        >批量修改状态</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>
    <el-table v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="订单编号" align="center" prop="orders" width="190">
        <template #default="scope">
          <div v-html="scope.row.orders.split(',').join('<br>')" />
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center" prop="activeName" min-width="120" />
      <el-table-column label="订单类型" align="center" prop="orderTypeStr" />
      <el-table-column label="开票金额" align="center" prop="total">
        <template #default="scope">
          ¥{{ scope.row.total }}
        </template>
      </el-table-column>
      <el-table-column label="发票来源" align="center" width="80">
        <template #default>
          酒店
        </template>
      </el-table-column>
      <el-table-column label="发票类型" align="center" prop="invoiceType" width="100">
        <template #default="scope">
          {{ invoiceTypeArr[scope.row.invoiceType] }}
        </template>
      </el-table-column>
      <el-table-column label="发票抬头" align="center" prop="invoiceTitle" min-width="130" />
      <el-table-column label="税号" align="center" prop="invoiceTfn" width="120" />
      <el-table-column label="电话" align="center" prop="invoicePhone" width="120" />
      <el-table-column label="开户行" align="center" prop="invoiceBank" />
      <el-table-column label="发票形式" align="center" prop="invoiceForm">
        <template #default="scope">
          {{ invoiceFormArr[scope.row.invoiceForm] }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱/邮寄地址" align="center" min-width="200" :show-overflow-tooltip="true">
        <template #default="scope">
          <div v-if="scope.row.invoiceForm=='1'">
            {{ scope.row.address }}
          </div>
          <div v-else>
            {{ scope.row.contact }}    {{ scope.row.phone }}
            <div :title="(scope.row.provinceName||'')+(scope.row.cityName||'')+(scope.row.cityName||'')+(scope.row.areaName||'')" class="hid160">
              {{ scope.row.provinceName }}{{ scope.row.cityName }}{{ scope.row.areaName }}{{ scope.row.lares }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="开票人" align="center" prop="createBy" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="申请时间" align="center" prop="createTime" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="开票状态" align="center" prop="isInvoice" width="80">
        <template #default="scope">
          {{ statusArr[scope.row.isInvoice] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            @click="handleChangeStatus(scope.row)"
          >开票状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <el-dialog v-model="showDialog" title="修改开票状态" append-to-body :close-on-click-modal="false" width="600px">
      <el-form>
        <el-form-item label="开票状态">
          <el-radio-group v-model="form.isInvoice">
            <el-radio label="0">未开票</el-radio>
            <el-radio label="1">已开票</el-radio>
            <!--          <el-radio label="2">取消开票</el-radio>-->
            <el-radio label="3">已寄出</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="showDialog=false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Logistics_Invoice">
import { listInvoice, makeInvoice } from '@/api/logex/invoice'
import { getCurrentInstance, ref, toRefs } from 'vue'
import { onMounted, reactive } from '@vue/runtime-core'
import { useRoute } from 'vue-router'
import usePermissionStore from '@/store/modules/permission'

const { proxy } = getCurrentInstance()
const route = useRoute()
const permissionStore = usePermissionStore()

const invoiceList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const showDialog = ref(false)
const ids = ref([])
const multiple = ref(true)
const isBelongMatch = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activeId: undefined,
    orders: undefined,
    activeName: undefined,
    invoiceType: undefined,
    invoiceTitle: undefined,
    invoiceTfn: undefined,
    regionId: undefined,
    invoicePhone: undefined,
    invoiceBank: undefined,
    invoiceAccount: undefined,
    invoiceForm: undefined,
    createBy: undefined,
    isInvoice: undefined,
    handTime: undefined,
    handTimeRange: undefined
  }
})

const { queryParams, form } = toRefs(data)

const invoiceTypeArr = ['企业', '个人非企业']
const invoiceFormArr = ['', '电子发票', '纸质普通', '纸质专票']
const statusArr = ['未开票', '已开票', '取消开票', '已寄出']

onMounted(() => {
  if (route.params.matchId) {
    permissionStore.setActiveMenu('/logistics/match')
    isBelongMatch.value = true
  }
})

/** 查询发票列表 */
function getList() {
  loading.value = true
  if (queryParams.value.handTime) {
    queryParams.value.handTimeRange = queryParams.value.handTime.join(',')
  } else {
    queryParams.value.handTimeRange = null
  }

  if (route.params.matchId) {
    queryParams.value.activeId = route.params.matchId
  }

  listInvoice(queryParams.value).then(response => {
    invoiceList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

function handleChangeStatus(row) {
  form.value = {
    id: [row.id],
    isInvoice: row.isInvoice
  }
  showDialog.value = true
}

function handleChangeStatusAll() {
  form.value = {
    id: ids,
    isInvoice: ''
  }
  showDialog.value = true
}

function submitForm() {
  makeInvoice(form.value).then(response => {
    proxy.$modal.msgSuccess('修改成功')
    showDialog.value = false
    getList()
  })
}

function handleExport() {
  if (queryParams.value.handTime) {
    queryParams.value.handTimeRange = queryParams.value.handTime.join(',')
  }

  proxy.download('/logex/invoice/export', {
    ...queryParams.value
  }, `开票信息_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<style lang="scss" scoped>
.hid160{text-overflow: ellipsis;white-space: nowrap;width: 160px;overflow: hidden;}
</style>
